package com.lin.missyou.api.v1;


import com.lin.missyou.bo.PageCounter;
import com.lin.missyou.core.LocalUser;
import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.dto.OrderDTO;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.logic.OrderChecker;
import com.lin.missyou.models.Order;
import com.lin.missyou.service.OrderService;
import com.lin.missyou.util.CommonUtil;
import com.lin.missyou.vo.OrderIdVO;

import com.lin.missyou.vo.OrderPureVO;
import com.lin.missyou.vo.OrderSimplifyVO;
import com.lin.missyou.vo.PagingDozer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/order")
@Validated
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Value("${missyou.order.pay-time-limit}")
    private long payTimeLimit;

    @PostMapping("")
    @ScopeLevel()
    public OrderIdVO placeOrder(@RequestBody OrderDTO orderDTO){
        /*
            校验参数:
                1 商品 无货
                2 最大购买数量，总数量限制 单个sku限制
                3 totalprice
                4 finaltotalprice
                5 是否拥有优惠券
                6 优惠券是否过期
         */
        Long uid = LocalUser.getUser().getId();
        OrderChecker orderChecker = this.orderService.isOk(uid,orderDTO);
        Long oid = this.orderService.placeOrder(uid,orderDTO,orderChecker);
        return new OrderIdVO(oid);
    }

    @ScopeLevel()
    @GetMapping("/status/unpaid")
    public PagingDozer getUnpaid(@RequestParam(defaultValue =  "0") Integer start,@RequestParam(defaultValue = "10") Integer count){
        PageCounter page = CommonUtil.convertToPageParameter(start,count);
        Page<Order> orderPage = this.orderService.getUnpaid(page.getPage(), page.getCount());
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        pagingDozer.getItems().forEach((o)->((OrderSimplifyVO) o).setPeriod(this.payTimeLimit));
        return pagingDozer;
    }


    @ScopeLevel()
    @GetMapping("/by/status/{status}")
    public PagingDozer geByStatus(@PathVariable int status,
                                  @RequestParam(name = "start",defaultValue = "0") Integer start,
                                  @RequestParam(name="count",defaultValue = "10") Integer count
    ){
        PageCounter page = CommonUtil.convertToPageParameter(start,count);
        Page<Order> orderPage = this.orderService.getByStatus(page.getPage(), page.getCount(),status);
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        pagingDozer.getItems().forEach((o)->((OrderSimplifyVO) o).setPeriod(this.payTimeLimit));
        return pagingDozer;
    }


    @ScopeLevel()
    @GetMapping("/detail/{id}")
    public OrderPureVO getOrderDetail(@PathVariable(name = "id") Long oid
    ){
        Optional<Order> orderOptional = this.orderService.getDetail(oid);
        return orderOptional.map(o-> new OrderPureVO(o,this.payTimeLimit)).orElseThrow(()->new NotFoundException(50006));
    }
}
